<?php
	//通过Wechat类， 创建一个对象
	$wechatObj = new Wechat();
	
	/*--------------------网址接入------------------------*/
	//接收数据
	$signature = $_GET['signature'];
	$timestamp = $_GET['timestamp'];
	$nonce = $_GET['nonce'];
	$token = "weixin";
	
	//如果没有通过GET收到echostr字符串， 说明不是再使用token验证
	if (!isset($_GET['echostr'])) {
		//调用wecat对象中的方法响应用户消息
   		$wechatObj->responseMsg();
	}else{
		//调用valid()方法，进行token验证
  		$wechatObj->valid();
	}
	
class Wechat 
{
	public function valid()
    {
		//1. 将token、timestamp、nonce三个参数进行字典序排序
		$arr = array($token,$timestamp,$nonce);
		
		sort($arr);
		
		//2.组装成一个字符串
		$str = join($arr);
		
		//3.使用sha1加密
		$str = sha1($str);
		
		//与signature对比，标识该请求来源于微信
		if($str==$signature){
		
			echo $echostr;
		
		}
	}
	
	public function responseMsg()
	{
		/*------------------------处理数据-------------------------*/
		
		//接收post数据
		$data = $GLOBALS["HTTP_RAW_POST_DATA"];
		
		//将这个字符串处理成xml对象
		$xmlobj = simplexml_load_string($data,'SimpleXMLElement',LIBXML_NOCDATA);
		
		//通过这个对象，获取对象当中的属性
		$touser = $xmlobj->ToUserName;
		$fromuser = $xmlobj->FromUserName;
		$createtime = $xmlobj->CreateTime;
		$type = $xmlobj->MsgType;
		
		
		/*-----------------------将数据插入到数据库当中----------------------------*/
		
		if($type=="text"){//如果用户发送的是一个文本信息
		
			$content = $xmlobj->Content;
			
			$tpl = "<xml>
						 <ToUserName><![CDATA[%s]]></ToUserName>
						 <FromUserName><![CDATA[%s]]></FromUserName>
						 <CreateTime>%s</CreateTime>
						 <MsgType><![CDATA[%s]]></MsgType>
						 <Content><![CDATA[%s]]></Content>
						 <FuncFlag>0</FuncFlag>
						</xml>";
				
			$toUser = $fromuser;
			$fromUser = $touser;
			$time = time();
			$MsgType = "text";
			
			//判断个人账户给公众账号发送的内容
				
			if($content=="1" || $content=="微博"){
			
				$reply = "我的微博:http://weibo.com/u/3161154113";
				
			}elseif($content=="2"){
			
				$reply = "我的个人网址：http://user.qzone.qq.com/353996852/main";
			
			}elseif($content=="3" || $content=="qq"){
			
				$reply = "我的QQ：353996852";
			
			}else{
			  $reply = "未找到相应服务，请重试";
			}
			
			$to_user = $xmlobj->ToUserName;
			$from_user = $xmlobj->FromUserName;
			$ptime = $xmlobj->CreateTime;
			$type = $xmlobj->MsgType;
			$content = $xmlobj->Content;
			$id = $xmlobj->MsgId;
			
			include "./db_conf.php";
			
			try{
			
				$dsn = "mysql:host=$db_host;dbname=$db_name";
		
			    $pdo = new PDO($dsn,$db_user,$db_pass);
			
			    $pdo->query("set names utf8");
				
				$pdo->exec("delete from weixin");
				
				$sql = "insert into weixin(id,to_user,from_user,ptime,content) values('$id','$to_user','$from_user','$ptime','$content')";

				$pdo->exec($sql);
				
			}catch(PDOException $e){
			
				exit($e->getMessage());
			
			}
			
			//绑定模板
			
			$str = sprintf($tpl,$toUser,$fromUser,$time,$MsgType,$reply);
			
			if(!empty($str)){
			
				echo $str;//输出xml内容
			
			}
		
		}
		
		//如果用户执行的是一个事件
		
		if($type=="event"){
		
			$event = $xmlobj->Event;
			
			if($event=="subscribe"){//如果用户发生了一次关注事件
			
				$tpl = "<xml>
							 <ToUserName><![CDATA[%s]]></ToUserName>
							 <FromUserName><![CDATA[%s]]></FromUserName>
							 <CreateTime>%s</CreateTime>
							 <MsgType><![CDATA[%s]]></MsgType>
							 <Content><![CDATA[%s]]></Content>
							 <FuncFlag>0</FuncFlag>
							</xml>";
				
				$toUser = $fromuser;
				$fromUser = $touser;
				$time = time();
				$MsgType = "text";
				
				$reply = '欢迎关注强哥的微信测试号<a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxfd242a7284b3b542&redirect_url=http://123.206.24.226/weixin/oauth2.php&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect">点击这里体验OAuth授权</a>';
				
				$str = sprintf($tpl,$toUser,$fromUser,$time,$MsgType,$reply);
				
				if(!empty($str)){
				
					echo $str;
				
				}
			}
		}
	}
}

	